package com.yanqu.road.server.logic.bag;

import com.yanqu.road.entity.bag.UseGoodsResult;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.player.UserBeauty;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.BeautyModule;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.server.manger.log.LogMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.math.BigInteger;
import java.util.*;

/**
 * @Description 随机增加美女的才艺道具
 * @Author cwq
 * @Data 2021/9/9 9:44
 */
public class UseRandomAddBeautyExp  extends BaseUseGoods {
    public UseRandomAddBeautyExp(GamePlayer player, GoodsInfo goodsInfo, long param) {
        super(player, goodsInfo, param);
    }

    @Override
    int checkUse(long count) {
        return 0;
    }

    @Override
    UseGoodsResult executeUse(long count) {
        Random random = new Random();
        UseGoodsResult result = new UseGoodsResult();
        BeautyModule beautyModule = player.getModule(BeautyModule.class);
        List<UserBeauty> randomList = beautyModule.getRandomBeautyList();
        long isRandomExp = goodsInfo.getParamList().get(0).longValue();
        int occupation = goodsInfo.getParamList().get(1).intValue();
        int patronsId = (int) param;
        UserBeauty userBeauty = null;
        if(-1 == occupation){
            if(0 == param){
                result.setRet(GameErrorCode.E_BAG_ITEM_USE_NO_SET_BEAUTY);
                return result;
            }
            userBeauty = beautyModule.getUserBeauty(patronsId);
            if(null == userBeauty){
                result.setRet(GameErrorCode.E_BEAUTY_NO_FOUND);
                return result;
            }
            //判断才艺等级是否满级
            if (beautyModule.checkEscortLevelFull(userBeauty)) {
                result.setRet(GameErrorCode.E_BEAUTY_ESCORT_LEVEL_FULL);
                return result;
            }
        }else {
            if(randomList.size() == 0 ){
                result.setRet(GameErrorCode.E_BAG_THERE_ARE_NO_RANDOM_BEAUTIES);
                return result;
            }
        }
        Property property = new Property();
        List<UserBeauty> syncList = new ArrayList<>();

        // 标记美女被加了多少次
        Map<Integer, Integer> beautyAddNumMap = new HashMap<>();

        for(int i = 0; i < count; i++) {
            long addExp = 0;
            if(0 == isRandomExp){
                addExp = GoodsMgr.getAddRandomValue(goodsInfo).longValue();
            }else{
                addExp = isRandomExp;
            }
            UserBeauty randomBeauty = userBeauty;
            if(null == randomBeauty) {
                randomBeauty = randomList.get(random.nextInt(randomList.size()));
            }
            if(null != randomBeauty) {

                property.addProperty(randomBeauty.getBeautyId(), BigInteger.valueOf(addExp));
                if(!syncList.contains(randomBeauty)){
                    syncList.add(randomBeauty);
                }
                if (!beautyAddNumMap.containsKey(randomBeauty.getBeautyId())) {
                    beautyAddNumMap.put(randomBeauty.getBeautyId(), 0);
                }
                beautyAddNumMap.put(randomBeauty.getBeautyId(), beautyAddNumMap.get(randomBeauty.getBeautyId()) + 1);



            }
        }

        for (Map.Entry<Integer, BigInteger> entry : property.getGoods().entrySet()) {
            int addBeautyId = entry.getKey();
            long addValue = entry.getValue().longValue();
            UserBeauty addUserBeauty = beautyModule.getUserBeauty(addBeautyId);
            if (addUserBeauty != null) {
                long oldValue = addUserBeauty.getEscortExp();
                addUserBeauty.setEscortExp(addUserBeauty.getEscortExp() + addValue);
                LogMgr.addLogUseAddAttributeGoods(player.getUserId(), goodsInfo.getGoodsId(), goodsInfo.getType(), addBeautyId, addValue,
                        BigInteger.valueOf(oldValue), BigInteger.valueOf(addUserBeauty.getEscortExp()), beautyAddNumMap.getOrDefault(addBeautyId, 0));
//                //todo：美女押镖经验优化。先不上。经验转换的道具，不参与冲榜
//                if (goodsInfo.getGoodsId() != GameConfig.ESCORT_EXP_TRANSFORM_PROP_ID) {
//                    player.notifyListener(eGamePlayerEventType.BeautyEscortAddExpRank.getValue(), addValue);
//                }
            }

        }
        if(syncList.size() > 0){
            beautyModule.onBeautyListChange(syncList);
            beautyModule.syncUserBeauty(syncList,false);
        }
        result.setReward(PropertyHelper.parsePropertyToString(property));
        return result;
    }
}
